﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class JCCDMXB_Query : BasePage
    {
        JCCDMXB_Dao dao = new JCCDMXB_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                this.hdJCCDID.Value = Request.QueryString["id"];
                string title = "进出仓单明细列表";
                this.lbTitle.Text = title;
                InitLabels();
                BindGridview();
            }
        }

        private void InitLabels()
        {
            string jccdid = this.hdJCCDID.Value;
            JCCD_Dao dao = new JCCD_Dao();
            JCCD jccd = dao.Query(jccdid);

            this.lblBH.Text = jccd.BH;
            this.lblLX.Text = GetDJLX(jccd.LX);

            Departments_Dao daoDep = new Departments_Dao();
            Department dep = daoDep.Query(jccd.BMID.ToString());
            this.lblBM.Text = dep.Name;

            Users_Dao daoUser = new Users_Dao();
            User user = daoUser.Query(jccd.RYID.ToString());
            if (user != null)
            {
                this.lblRY.Text = user.Name;
            }

            User cgy = daoUser.Query(jccd.CGYID.ToString());
            if (cgy != null)
            {
                this.lblCGY.Text = cgy.Name;
            }

            this.lblRQ.Text = jccd.RQ.ToString("yyyy年MM月hh日 HH:mm:ss");
            // this.lblYY.Text = jccd.YY; TODO fuck
            this.lblGLDJ.Text = jccd.GLDJID;

            if (jccd.ZT == 0)
            {
                this.btnConfirm.Enabled = true;
            }
            else
            {
                this.btnConfirm.Text = "已确认";
                this.btnConfirm.Enabled = false;
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            // 产品 
            ZL_CP_Dao daoCp = new ZL_CP_Dao();
            List<ZL_CP> listcp = daoCp.QueryAll();

            // 物料
            ZL_WL_Dao daoWl = new ZL_WL_Dao();
            List<ZL_WL> listwl = daoWl.QueryAll();

            GridView1.PageIndex = RestorePageIndex();

            List<vJCCDMXB> list = dao.QueryByJCCDIDEx(this.hdJCCDID.Value);
            List<JCCDMXB_Ex> listEx = new List<JCCDMXB_Ex>();

            foreach (vJCCDMXB c in list)
            {
                JCCDMXB_Ex ce = new JCCDMXB_Ex();
                ce.CKBH = c.CKBH;
                ce.CKID = c.CKID;
                ce.CKMC = c.CKMC;
                ce.CWBH = c.CWBH;
                ce.DJ = c.DJ;
                ce.DJLX = c.DJLX;
                ce.DWBH = c.DWBH;
                ce.DWMC = c.DWMC;
                ce.ID = c.ID;
                ce.JCCDID = c.JCCDID;
                ce.SL = c.SL;
                ce.WPID = c.WPID;
                ce.WPLX = GetWPLX(c.WPLX);

                ce.JCSJ = c.JCSJ.ToString("yyyy-MM-dd HH:ss:dd");

                if (c.WPLX == "01")
                {
                    ZL_WL wl = listwl.Find(delegate(ZL_WL p) { return p.ID == c.WPID; });
                    ce.WPBH = wl.BH;
                    ce.WPMC = wl.MC;
                }
                else if (c.WPLX == "02")
                {
                    ZL_CP cp = listcp.Find(delegate(ZL_CP p) { return p.ID == c.WPID; });
                    ce.WPBH = cp.BH;
                    ce.WPMC = cp.BH;
                }

                listEx.Add(ce);
            }

            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        protected class JCCDMXB_Ex
        {
            public Guid ID { get; set; }
            public Guid CKID { get; set; }
            public string CKBH { get; set; }
            public string CKMC { get; set; }
            public Guid CWID { get; set; }
            public string CWBH { get; set; }

            public double DJ { get; set; }
            public string DJLX { get; set; }
            
            public string DWBH { get; set; }
            public string DWMC { get; set; }

            public string JCSJ { get; set; }
            public Guid JCCDID { get; set; }
            public double SL { get; set; }
            public Guid WPID { get; set; }
            public string WPLX { get; set; }
            public string WPBH { get; set; }
            public string WPMC { get; set; }
        }

        private string GetWPLX(string key)
        {
            string name = "";
            switch (key.Trim())
            {
                case "01":
                    name = "物料";
                    break;
                case "02":
                    name = "产品";
                    break;
                case "03":
                    name = "部件";
                    break;
            }

            return name;
        }

        private string GetDJLX(string key)
        {
            string name = "";
            switch (key.Trim())
            {
                case "0001":
                    name = "物料采购进仓";
                    break;
                case "0002":
                    name = "物料调拨进仓";
                    break;
            }
            return name;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((LinkButton)(e.Row.Cells[9].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string jccdid = this.hdJCCDID.Value;
            JCCD_Dao dao = new JCCD_Dao();
            JCCD jccd = dao.Query(jccdid);
            bool res = false;
            if (jccd.LX.CompareTo("1000") < 0)
            {
                res = dao.ConfirmIn(jccdid);
            }
            else
            {
                List<Guid> less = new List<Guid>();
                res = dao.ConfirmOut(jccdid, ref less);
            }

            if(res) 
            {
                lbResult.Text = "确认单据成功。";
                this.btnConfirm.Text = "已确认";
                this.btnConfirm.Enabled = false;
            }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("~/Page/JCCDMXB_Add.aspx?jccdid=" + this.hdJCCDID.Value);
        }

    }
}